package com.rainxing.cockpit.modules.quartz.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.quartz.utils.ConnectionProvider;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @Auther: ybzhu
 * @Date: 2018/7/19 16:38
 * @Description:
 */
public class QuartzDurid implements ConnectionProvider {

    private String driver;
    private String URL;
    private String username;
    private String password;
    private DruidDataSource dataSource;

    @Override
    public Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    @Override
    public void shutdown() throws SQLException {
        dataSource.close();
    }

    @Override
    public void initialize() throws SQLException {
        if (this.URL == null) {
            throw new SQLException("url null");
        }
        if (this.driver == null) {
            throw new SQLException("driver null");
        }
        if (this.driver == null) {
            throw new SQLException("driver null");
        }
        if (this.password == null) {
            throw new SQLException("password null");
        }
        dataSource = new DruidDataSource();
        try {
            dataSource.setDriverClassName(this.driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
        dataSource.setUrl(this.URL);
        dataSource.setUsername(this.username);
        dataSource.setPassword(this.password);
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getURL() {
        return URL;
    }

    public void setURL(String URL) {
        this.URL = URL;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public DruidDataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DruidDataSource dataSource) {
        this.dataSource = dataSource;
    }
}
